/**
 * 
 */
define(function(){
	var m_counter = 0;
	var m_conf = 0;		//0--none, 1--to initconference, 3--to holdcall on establish, 2--held, <0--fail
	var m_addedparty = {};
	var m_deletedparty = '';
	var m_holdsilence = 0;
	
	var Extra = function(project){
		this.project = project;
		this.name = 'ctiextra_conferenceivr';
		$SC.regcall(this.name, [['queryextra', this.queryCTICall]]);
		$SC.addEvtListener(this.name, 'OnOrigCTIEvent', OnCTIEvent, -1);
		$SC.addEvtListener(this.name, 'OnSCTIEvent', OnSCTIEvent, -1);
	}
	Extra.prototype = {
		queryCTICall : function(callName, callParams, callback){
			if (callParams.callname) {
				if (callParams.callname.toLowerCase() == 'conferenceivr') {
					m_counter++;
					var dest = callParams.dest;
				    //var param = 'AutoComplete=1|'+callParams.params;
					var param = 'CompleteWhenEstablish=1|'+callParams.params;
					
				    m_conf = 1;
				    m_addedparty = {};
				    m_deletedparty = '';
				    $SC.addconsolelog('ctiextra_confivr: initConference() with counter = ' + m_counter + ', m_conf = ' + m_conf)
				    $SC.initConference(dest, param);
            		setTimeout(function(counter){
            			if (counter == m_counter && m_conf == 1) {
        				    $SC.addconsolelog('ctiextra_confivr: Timeout with counter = ' + counter + ', m_conf = ' + m_conf)
                			m_conf = -9;
            			}
            		}, 5000, m_counter);
				}
			}
		}
	}

	function OnCTIEvent(evt){
		var e = evt.event.value.toString().toLowerCase();
		if (m_conf > 0) {
			$SC.addconsolelog('ctiextra_confivr: event = ' + e + ', m_conf = ' + m_conf + ', CallState2 = ' + $SC.CallInfo.CallState2)
		} else {
			return;
		}
		switch(e){
			//EVT_CONFERENCED : 21,
			case '10021':
			case '21':
			//EVT_PARTYADDED : 8
			case '8':
				if (m_conf > 0) {
					if (evt.addedparty) {
						m_addedparty[evt.addedparty.value] = evt;
						if (evt.silentmonitor) {
							$SC.addconsolelog('ctiextra_confivr: addedparty = ' + evt.addedparty.value + ', silentmonitor = ' + evt.silentmonitor.value);
						} else {
							$SC.addconsolelog('ctiextra_confivr: addedparty = ' + evt.addedparty.value + '');
						}
					}
					if (evt.thirdpartydn && !m_addedparty[evt.thirdpartydn.value]) {
						m_addedparty[evt.thirdpartydn.value] = evt;
						if (evt.silentmonitor) {
							$SC.addconsolelog('ctiextra_confivr: addedparty = ' + evt.thirdpartydn.value + ', silentmonitor = ' + evt.silentmonitor.value);
						} else {
							$SC.addconsolelog('ctiextra_confivr: addedparty = ' + evt.thirdpartydn.value + '');
						}
					}
				}
				//break;
			case '2':
				if ($SC.CallInfo.CallState2 == 2) {
					if (m_conf == 1) {
						$SC.addconsolelog('ctiextra_confivr: m_conf = 1 --> 3, to holdCall(), HoldSilence = ' + m_holdsilence);
						m_conf = 3;
						var params = {};
						if (m_holdsilence == 1) {
							params.holdsilence = 1;
						}
						$SC.holdCall(params);
						setTimeout(function(){
							if (m_conf == 3) {
								$SC.addconsolelog('ctiextra_confivr: m_conf = 3 --> 2, after holdCall()');
								m_conf = 2;
							} else {
								$SC.addconsolelog('ctiextra_confivr: m_conf = ' + m_conf + ', after holdcall()');
							}
						}, 500);
					} else if (m_conf == 2) {
						$SC.addconsolelog('ctiextra_confivr: m_conf = ' + m_conf + ' --> 0, for event ' + e);
						m_conf = 0;
					}
				}
				break;
			//EVT_RETRIEVED : 7
			case '7':
				if (m_conf == 2 || m_conf == 3) {
					$SC.addconsolelog('ctiextra_confivr: m_conf = ' + m_conf + ' --> 0, for event ' + e);
					m_conf = 0;
					if (m_deletedparty) {
						$SC.addconsolelog('ctiextra_confivr: releaseCall() for m_deletedparty = ' + m_deletedparty);
						$SC.releaseCall();
					}
				}
				break;
			//EVT_PARTYDELETED : 9,
			case '9':
				if (m_conf) {
					var thirddn = '';
					if (evt.thirdpartydn) {
						thirddn = evt.thirdpartydn.value;
					} else if(evt.deletedparty) {
					    thirddn = evt.deletedparty.value;
                    }
					if (thirddn) {
						if (m_addedparty[thirddn]) {
							if (m_addedparty[thirddn].silentmonitor) {
								$SC.addconsolelog('ctiextra_confivr: EVT_PARTYDELETED ignored, silentmonitor = 1 for ' + thirddn);
								break;
							} else {
								$SC.addconsolelog('ctiextra_confivr: EVT_PARTYDELETED for ' + thirddn);
							}
						} else if (thirddn.substr(0,1).toLowerCase() == 't') {
							$SC.addconsolelog('ctiextra_confivr: EVT_PARTYDELETED for strange ' + thirddn);
						} else if (thirddn == $SC.CallInfo.ANI || thirddn == $SC.CallInfo.DNIS) {
							$SC.addconsolelog('ctiextra_confivr: EVT_PARTYDELETED ' + thirddn + ', ANI = ' + $SC.CallInfo.ANI + ', DNIS = ' + $SC.CallInfo.DNIS);
						} else {
							$SC.addconsolelog('ctiextra_confivr: EVT_PARTYDELETED ignored for unmatched ' + thirddn);
							break;
						}
					}
					if ($SC.CallInfo.CallState2 == 3) {
						//$SC.addconsolelog(JSON.stringify(evt));
						var calling = $SC.CallInfo.ANI;
						//if (evt.deletedparty) {
						if (thirddn) {
							if ($SC.CallInfo.CallType == 3) {
								calling = $SC.CallInfo.DNIS;
							}
							//if (evt.deletedparty.value == calling) {
							//if (thirddn == calling || thirddn.substr(0,1).toLowerCase() == 't') {
							if (thirddn == calling) {
							} else {
								calling = '';
							}
							//if (!calling && thirddn.substr(0,1).toLowerCase() != 't') {
							/*
							if (!calling) {
								if (!m_addedparty[thirddn] || m_addedparty[thirddn].silentmonitor) {
									$SC.addconsolelog('ctiextra_confivr: no RetrieveCall() on EVENT_PARTYDELETED, for deletedparty = ' + thirddn);
									break;
								} 
							} else if (thirddn.substr(0,1).toLowerCase() == 't' && calling != thirddn) {
								$SC.addconsolelog('ctiextra_confivr: no RetrieveCall() on EVENT_PARTYDELETED, for deletedparty = ' + thirddn);
								break;
							}
							*/
						} else {
							calling = '';
						}
						m_deletedparty = calling;
						if (m_deletedparty) {
							$SC.addconsolelog('ctiextra_confivr: deleted party is ' + m_deletedparty + ', retrieveCall() and then releaseCall()');
						} else {
							$SC.addconsolelog('ctiextra_confivr: retrieveCal()');
						}
						$SC.retrieveCall();
					}
				}
				break;
		}
	}

	function OnSCTIEvent(evt) {
		switch(evt.toLowerCase()){
			case 'onlogin':
				m_holdsilence = $SC._getconfig("ConferenceIVR", "HoldSilence") || 0;
				break;
			case 'oncallstart':
			case 'oncallend':
				if (m_conf) {
					$SC.addconsolelog('ctiextra_confivr: m_conf = ' + m_conf + ' --> 0, for event ' + evt.toLowerCase());
					m_conf = 0;
				}
				m_addedparty = {};
				break;
		}
	}
	return Extra; 
});

